﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.DishOrderModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.DishOrderModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.DishOrderModule.FacadeObject
{
    /// <summary>
    /// 菜肴订单FO
    /// </summary>
    public class DishOrderFO : IApplicationFacadeObject
    {
        #region 私有属性

        private DishOrderDQO _DishOrderDQO;
        private DishOrderDQO DishOrderDQO
        {
            get
            {
                if (_DishOrderDQO == null)
                    _DishOrderDQO = ObjectCreator.Create<DishOrderDQO>();

                return _DishOrderDQO;
            }
        }

        private WeekDishOrderDQO _WeekDishOrderDQO;
        private WeekDishOrderDQO WeekDishOrderDQO
        {
            get
            {
                if (_WeekDishOrderDQO == null)
                    _WeekDishOrderDQO = ObjectCreator.Create<WeekDishOrderDQO>();

                return _WeekDishOrderDQO;
            }
        }

        #endregion

        #region 补充订单和特殊订单方法

        public virtual void AddDishOrder(DishOrderDTO orderDTO,DishOrderState orderState = DishOrderState.Publish)
        {
            DishOrder order = orderDTO.ToDishOrder();
            order.OrderState = orderState.ToString();
            order.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            orderDTO.ID = order.ID;
        }

        public virtual void UpdateDishOrder(DishOrderDTO orderDTO,DishOrderState orderState = DishOrderState.Publish)
        {
            DishOrder order = DishOrderDQO.GetDishOrder(orderDTO.ID);
            order.OrderState = orderState.ToString();
            orderDTO.CopyToDishOrder(order);

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteDishOrder(string id)
        {
            DishOrder order = DishOrderDQO.GetDishOrder(id);

            order.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();
        }

        public virtual DishOrderDTO GetDishOrderDTO(string id)
        {
            DishOrder order = DishOrderDQO.GetDishOrder(id);
           
            return new DishOrderDTO(order);
        }

        
        
        #endregion

        #region 每周订单方法

        /// <summary>
        /// 上报一周菜单
        /// </summary>
        /// <param name="weekDTO"></param>
        /// <param name="items"></param>
        public virtual void AddWeekDishOrder(WeekDishOrderDTO weekDTO, DishOrderDTO[] items, DishOrderState orderState = DishOrderState.Publish)
        {
            WeekDishOrder weekOrder = weekDTO.ToWeekDishOrder();
            weekOrder.OrderState = orderState.ToString();

            foreach (DishOrderDTO itemDTO in items)
            {
                DishOrder order = itemDTO.ToDishOrder();
                order.OrderState = orderState.ToString();
                weekOrder.AddOrder(order);
            }

            weekOrder.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            weekDTO.ID = weekOrder.ID;
        }

        /// <summary>
        /// 修改一周菜单
        /// </summary>
        /// <param name="weekDTO"></param>
        /// <param name="items"></param>
        /// <param name="orderState"></param>
        public virtual void UpdateWeekDishOrder(WeekDishOrderDTO weekDTO, DishOrderDTO[] items, DishOrderState orderState = DishOrderState.Publish)
        {
            WeekDishOrder weekOrder = WeekDishOrderDQO.GetWeekDishOrder(weekDTO.ID);
            weekOrder.OrderState = orderState.ToString();

            foreach (DishOrderDTO itemDTO in items)
            {
                DishOrder order = weekOrder.GetOrder(itemDTO.ID);
                order.OrderState = orderState.ToString();
                itemDTO.CopyToDishOrder(order);                
            }

            weekOrder.Update();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        /// <summary>
        /// 获取一周菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        public virtual WeekDishOrderDTO GetWeekDishOrderDTO(string id, out DishOrderDTO[] items)
        {
            WeekDishOrder weekOrder = WeekDishOrderDQO.GetWeekDishOrder(id);
            WeekDishOrderDTO weekDTO = new WeekDishOrderDTO(weekOrder);

            List<DishOrderDTO> list = new List<DishOrderDTO>();
            foreach (DishOrder item in weekOrder.DishOrders)
            {
                DishOrderDTO orderDTO = new DishOrderDTO(item);
                list.Add(orderDTO);
            }

            items = list.ToArray();

            return weekDTO;
        }

        #endregion

    }
}
